<!doctype html>
<html lang="en">
	<head>
		<title>Tween.js / hello world!</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div id="info">
			<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
			<h2>18 _ start from current values</h2>
			<p>Example for illustrating the startFromCurrentValues() functionality.</p>
			<button id="start-tween">Start tween</button>
			<button id="up">↑</button>
			<button id="down">↓</button>
			<button id="left">←</button>
			<button id="right">→</button>
		</div>
		<div
			id="target"
			style="
				position: absolute;
				transform: translate(400px, 200px);
				width: 100px;
				height: 100px;
				background: #a0dde9;
				padding: 1em;
			"
		></div>

		<script type="module">
			import * as TWEEN from '../dist/tween.esm.js'

			const position = {x: 400, y: 200}
			const target = document.getElementById('target')
			const tween = new TWEEN.Tween(position)
				.to({x: 700, y: 200}, 2000)
				.easing(TWEEN.Easing.Elastic.InOut)
				.onUpdate(update)

			document.getElementById('start-tween').addEventListener('click', () => {
				tween.startFromCurrentValues() // start the tween from its current position
				animate() // call animate as we unregister the handler if TWEEN.update(time) returns false
			})

			registerArrowButtonEventListeners()

			animate()

			function animate(time) {
				if (tween.update(time)) requestAnimationFrame(animate)
			}

			function update() {
				target.style.transform = `translate(${position.x}px, ${position.y}px)`
			}

			//Add event listeners to each of the arrow buttons
			function registerArrowButtonEventListeners() {
				document.getElementById('up').addEventListener('click', moveUp)
				document.getElementById('down').addEventListener('click', moveDown)
				document.getElementById('left').addEventListener('click', moveLeft)
				document.getElementById('right').addEventListener('click', moveRight)
			}

			// Arrow button callbacks
			function moveLeft() {
				position.x -= 20
				update()
			}

			function moveRight() {
				position.x += 20
				update()
			}

			function moveUp() {
				position.y -= 20
				update()
			}

			function moveDown() {
				position.y += 20
				update()
			}
		</script>
	</body>
</html>
